package com.qf.work02;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.BufferedInputStream;
import java.io.IOException;

@WebServlet("/loninc")
public class LoginServlet01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
       String user = (String) session.getAttribute("user");

        if (user!=null){

            BufferedInputStream is = FileUtil.getInputStream("E:\\Program Files\\img\\二次元.jpg");
            ServletOutputStream os = resp.getOutputStream();
            IoUtil.copy(is,os,IoUtil.DEFAULT_BUFFER_SIZE);
        }else {
            Cookie[] cookies = req.getCookies();
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if (name.equals("myc")){
                    BufferedInputStream is = FileUtil.getInputStream("E:\\Program Files\\img\\二次元.jpg");
                    ServletOutputStream os = resp.getOutputStream();
                    IoUtil.copy(is,os,IoUtil.DEFAULT_BUFFER_SIZE);
                    return;
                }
            }
            BufferedInputStream is = FileUtil.getInputStream("E:\\Program Files\\img\\二次元黑.jpg");
            ServletOutputStream os = resp.getOutputStream();
            IoUtil.copy(is,os,IoUtil.DEFAULT_BUFFER_SIZE);
        }

    }
}
